/**
 * 
 */
package com.playball.lottery.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.base.BaseActivity;


/**
 * @ClassName: AbstractActivity
 * @Description: TODO(公共activity)
 * @author SZW vanszhen@gmail.com
 * @date 2013-6-6 15:12:00
 * @version 版本1.0
 */
/**
 * 
 * @ClassName: AbstractActivity
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author szw vanszhen@gmail.com
 * @date 2014-2-21 下午4:11:39 V2.0
 */
public class AbstractActivity extends BaseActivity {
	public static Context context;
	protected int screenwidth;
	protected int screenheight;
	protected float screendensity;
	protected int screendensityDpi;
	private Dialog dialog;

	/** 底部弹出的Dialog的View. */
	private View mBottomDialogView = null;

	/** 居中弹出的Dialog的View. */
	private View mCenterDialogView = null;

	/** 顶部弹出的Dialog的View. */
	private View mTopDialogView = null;

	/** 底部弹出的Dialog. */
	private Dialog mBottomDialog;

	/** 居中弹出的Dialog. */
	private Dialog mCenterDialog;

	/** 顶部弹出的Dialog. */
	private Dialog mTopDialog;

	/** 弹出的Dialog的左右边距. */
	private int dialogPadding = 40;

	/** 屏幕宽度. */
	public int diaplayWidth = 320;

	/** 屏幕高度. */
	public int diaplayHeight = 480;
	/** Window 管理器. */
	private WindowManager mWindowManager = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		screenwidth = metrics.widthPixels;// 屏幕宽度（像素）
		screenheight = metrics.heightPixels;// 屏幕高度（像素）
		screendensity = metrics.density;// 屏幕密度（0.75 / 1.0 / 1.5）
		screendensityDpi = metrics.densityDpi;// 屏幕密度DPI（120 / 160 / 240）

		mWindowManager = getWindowManager();
		Display display = mWindowManager.getDefaultDisplay();
		diaplayWidth = display.getWidth();
		diaplayHeight = display.getHeight();
	}

    /**
     * 判断网络 所有的的网络 是否已经连接
     *
     * @Title: isNetworkAvailable
     * @Description: TODO(判断网络)
     * @return boolean 返回类型
     */
    public  boolean isNetworkAvailable() {

		Context context = getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] infos = connectivity.getAllNetworkInfo();
            if (infos != null) {
                for (int i = 0; i < infos.length; i++) {
                    if (infos[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

	/**
     * 判断网络 所有的的网络 是否已经连接
     *
     * @Title: isNetworkAvailable
     * @Description: TODO(判断网络)
     * @return boolean 返回类型
     */
    public static boolean isNetworkAvailable(Context context) {

//		Context context = getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] infos = connectivity.getAllNetworkInfo();
            if (infos != null) {
                for (int i = 0; i < infos.length; i++) {
                    if (infos[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

	/**
	 * 设置网络
	 * 
	 * @Title: setNetWork
	 * @Description: TODO(设置网络)
	 */
	public static void setNetWork() {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.net_message);
		builder.setPositiveButton(R.string.net_yes, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// 进入无线网络配置界面
				context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
			}
		});
		builder.setNegativeButton(R.string.net_no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				// 关闭当前activity
			}
		});
		builder.show();
	}

	/**
	 * 自定义Toast
	 * 
	 * @Title: definedToastShow
	 * @Description: TODO(自定义Toast)
	 * @param @param content 传人显示的Toast显示的内容
	 */
	/*
	 * public void definedToastShow(String content) { View toash =
	 * getLayoutInflater().inflate(R.layout.ui_android_notice, null); TextView
	 * textView = (TextView) toash.findViewById(R.id.textview_notice);
	 * textView.setText(content); Toast toast = new Toast(context);
	 * toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
	 * toast.setView(toash); toast.show(); }
	 */

	/**
	 * 描述：Toast提示文本.
	 * 
	 * @param text
	 *            文本
	 */
	public void showToast(String text) {
		Toast.makeText(this, "" + text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 描述：Toast提示文本.
	 * 
	 * @param resId
	 *            文本的资源ID
	 */
	public void showToast(int resId) {
		Toast.makeText(this, "" + this.getResources().getText(resId), Toast.LENGTH_SHORT).show();
	}

	/**
	 * 退程序 (关闭所有当前运行的Activity)
	 * 
	 * @Title: killApp
	 * @Description: TODO( )
	 */
	public void killApp() {
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	@Override
	protected void onResume() {
		context = this;
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	// waiting view while load data
	/**
	 * 弹出 进度条
	 */
	public void loadProgressDialog(String showWhat) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.dialogview, null);
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);
		TextView zi = (TextView) v.findViewById(R.id.pro_show_zi);
		if (AppUtils.isBlank(showWhat)) {
			zi.setText(R.string.Loading);
		} else {
			zi.setText(showWhat);
		}
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.animation);
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);

		dialog = new Dialog(context, R.style.FullHeightDialog);
		dialog.setCancelable(true);
		/*
		 * 将对话框的大小按屏幕大小的百分比设置
		 */
		Window dialogWindow = dialog.getWindow();
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int width = (int) ((metrics.widthPixels) * 0.6);
		Log.d("abs", "windth:" + width);
		// int height=metrics.heightPixels;
		WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
		layoutParams.width = width;
		dialogWindow.setAttributes(layoutParams);
		dialog.show();
		dialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
	}

	/**
	 * 销毁 进度条
	 */
	public void destoryDialog() {
		dialog.dismiss();
	}

	/**
	 * 描述：在底部显示一个Dialog,id为1,在中间显示id为2.
	 * 
	 * @param id
	 *            Dialog的类型
	 * @param view
	 *            指定一个新View
//	 * @see AbConstant.DIALOGBOTTOM
	 */
	public void showDialog(int id, View view) {

		if (id == 1) {
			mBottomDialogView = view;
			if (mBottomDialog == null) {
				mBottomDialog = new Dialog(this);
				setDialogLayoutParams(mBottomDialog, dialogPadding, Gravity.BOTTOM);
			}
			mBottomDialog.setContentView(mBottomDialogView, new LayoutParams(diaplayWidth - dialogPadding,
					LayoutParams.WRAP_CONTENT));
			showDialog(id);
		} else if (id == 2) {
			mCenterDialogView = view;
			if (mCenterDialog == null) {
				mCenterDialog = new Dialog(this);
				setDialogLayoutParams(mCenterDialog, dialogPadding, Gravity.CENTER);
			}
			mCenterDialog.setContentView(mCenterDialogView, new LayoutParams(diaplayWidth - dialogPadding,
					LayoutParams.WRAP_CONTENT));
			showDialog(id);
		} else if (id == 3) {
			mTopDialogView = view;
			if (mTopDialog == null) {
				mTopDialog = new Dialog(this);
				setDialogLayoutParams(mTopDialog, dialogPadding, Gravity.TOP);
			}
			mTopDialog.setContentView(mTopDialogView, new LayoutParams(diaplayWidth - dialogPadding,
					LayoutParams.WRAP_CONTENT));
			showDialog(id);
		} else {
			Log.i("tag", "Dialog的ID传错了，请参考AbConstant类定义");
		}
	}

	/**
	 * 描述：设置弹出Dialog的属性.
	 * 
	 * @param dialog
	 *            弹出Dialog
	 * @param dialogPadding
	 *            如果Dialog不是充满屏幕，要设置这个值
	 * @param gravity
	 *            the gravity
	 */
	private void setDialogLayoutParams(Dialog dialog, int dialogPadding, int gravity) {
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		Window window = dialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		// 此处可以设置dialog显示的位置
		window.setGravity(gravity);
		// 设置宽度
		lp.width = diaplayWidth - dialogPadding;
		lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
		// 背景透明
		// lp.screenBrightness = 0.2f;
		lp.alpha = 0.8f;
		lp.dimAmount = 0f;
		window.setAttributes(lp);
		// 添加动画
		window.setWindowAnimations(android.R.style.Animation_Dialog);
		// 设置点击屏幕Dialog不消失
		dialog.setCanceledOnTouchOutside(false);

	}
}
